package cn.stylefeng.guns.sys.core.listener;

import cn.stylefeng.guns.sys.modular.system.entity.BtyTpfk;
import cn.stylefeng.guns.sys.modular.system.service.BtyTpfkService;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/*
 * 监听器批处理数据
 * */
public class TpfkExcelListener extends AnalysisEventListener<BtyTpfk> {

    private BtyTpfkService tpfkService;

    public TpfkExcelListener(){

    }

    public TpfkExcelListener(BtyTpfkService tpfkService){
        this.tpfkService = tpfkService;
    }


    /**
     * 批处理阈值2000
     */
    private static final int BATCH_COUNT = 300;
    List<BtyTpfk> list = new ArrayList<>();

    /**
     *
     * @param basic
     * @param analysisContext
     */
    @Override
    public void invoke(BtyTpfk basic, AnalysisContext analysisContext) {
       // String mdid=basic.getMdid().replaceAll("\\s*", "");
        list.add(basic);
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        analysisContext.readWorkbookHolder().setDefaultReturnMap(false);
        saveData();
        // log.info("所有数据解析完成！");

    }
    private void saveData(){
        //调用saveData()方法
        tpfkService.insertTpfk(list);
    }
    /**
     * 这里会一行行的返回头
     *
     * @param headMap
     * @param context
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println(headMap);
    }
}